import sys

import psutil
import logging


def net_connections():
    net_connection = psutil.net_connections()
    return net_connection


def pids(pid):
    pid = psutil.pids()
    return pid


def process(pid):
    try:
        process = psutil.Process(pid)
    except Exception as e:
        return e
    return process


def process_name(pid):
    try:
        process_name = psutil.Process(pid).name()
    except Exception as e:
        return e
    return process_name


def process_exe(pid):
    try:
        process_exe = psutil.Process(pid).exe()
    except Exception as e:
        return e
    return process_exe


if __name__ == '__main__':
    net_connections = net_connections()
    for i in net_connections:
        processes = process(i.pid)
        data = "本地:{},远程:{},其他:{},进程名字:{},exe路径{}".format(i.laddr, i.raddr, processes, process_name(i.pid),
                                                          process_exe(i.pid))
        d = (i.laddr, i.raddr, processes, process_name(i.pid), process_exe(i.pid))

        print(data)
        # print(i.raddr, type(i.raddr))
